package utils.view
{
	import com.drm.utils.DRMAlert;
	
	import flash.net.FileReference;
	
	import flexlib.mdi.containers.MDIWindow;
	
	import mx.containers.TitleWindow;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.managers.CursorManager;
	import mx.managers.PopUpManager;
	import mx.messaging.messages.ErrorMessage;
	import mx.rpc.CallResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import services.ManagementService;
	import services.ReportService;
	
	import utils.SMContainerUtils;
	
	import valueObjects.ArquivoVO;
	
	public class SMPanelMax extends MDIWindow
	{
		private var dadosRelatorio:ArquivoVO;
		private var fileReference:FileReference;
		protected var processRelatorioResult:CallResponder;
		protected var managementService:ManagementService;
		protected var reportService:ReportService;
		private var smLoadStatus:SMLoadStatus = new SMLoadStatus();
		
		public function SMPanelMax()
		{
			super();
			
			processRelatorioResult = new CallResponder;
			processRelatorioResult.addEventListener(ResultEvent.RESULT, processRelatorioResult_resultHandler);
			processRelatorioResult.addEventListener(FaultEvent.FAULT, handleException);
			
			managementService = new ManagementService;
			managementService.addEventListener(FaultEvent.FAULT, handleException);
			
			reportService = new ReportService;
			reportService.addEventListener(FaultEvent.FAULT, handleException);
		}
		
		protected function processRelatorioResult_resultHandler(event:ResultEvent):void
		{
			removeWait();
			
			dadosRelatorio = event.result as ArquivoVO;
			
			if (dadosRelatorio != null)
			{
				DRMAlert.showConfirm(this, save, "Relatório gerado com sucesso! Deseja fazer o download do arquivo gerado?"); 
			} else
			{
				DRMAlert.show("Relatório não obteve nenhum resultado!");
			}
			
		}
		
		protected function save(event:CloseEvent):void
		{
			if(event.detail==Alert.YES)
			{
				fileReference = new FileReference();
				fileReference.save(dadosRelatorio.conteudo, dadosRelatorio.nome);
			}
		}
		
		public function handleException(event:FaultEvent):void{
			var errorMessage:ErrorMessage = event.message as ErrorMessage;
			DRMAlert.showErrorTitle(errorMessage.rootCause.message, 'Atenção');
		}
		
		public function validationRequiredFields(excludeFields:Array = null) : Boolean
		{
			
			var retorno:Boolean = SMContainerUtils.validationRequiredFieldsByContainer(this, excludeFields);
			
			if (!retorno)
			{
				DRMAlert.showErrorTitle('Campos obrigatórios não preenchidos!', 'Validação');
			}
			
			return retorno;
			
		}
		
		protected function addWait() : void
		{
			
			if (smLoadStatus != null)
			{
				smLoadStatus.show();
			}
			
			CursorManager.setBusyCursor();
		}
		
		protected function removeWait() : void
		{
			
			if (smLoadStatus != null)
			{
				smLoadStatus.hide();
			}
			
			CursorManager.removeBusyCursor();
		}
	}
}